

var globalData = {
    user_id : 48,
    token : "31e23adee5f9b3d16c0767289fcfae00",
    username : "明凯",
}



let init = function () {

    $.ajax({
            "url":"/api/talk/1",
            "method":"GET",
            data:{token:"31e23adee5f9b3d16c0767289fcfae00"},
            success:function (data) {
                talkRender(data["data"])
            }
        }
    )
}


var talkRender = function(data) {


    var tbody = document.createElement("tbody");
    var talkTab = document.getElementById("talkTab")


    for(let i = 0; i < data.length; i++) {

        var atr = document.createElement("tr");
        atr.setAttribute("index", i);

        var atd1 = document.createElement("td");
        atd1.innerHTML = data[i]["id"]

        var atd2 = document.createElement("td");
        atd2.innerHTML =  data[i]["content"].substr(0,50)

        var atd3 = document.createElement("td");
        atd3.innerHTML =  data[i]["pics"]

        var atd4 = document.createElement("td");
        atd4.innerHTML = data[i]["username"]

        var atd5 = document.createElement("td");
        atd5.innerHTML = data[i]["likes"]

        var atd6 = document.createElement("td");
        atd6.innerHTML =  data[i]["time"]

        var atd7 = document.createElement("td");

        var abtn = document.createElement("button")
        abtn.setAttribute("class", "deleteBtn");
        abtn.addEventListener("click", function (ev) {

            deleteTalk(data[i]["id"]);
        });
        abtn.innerHTML = "删除"
        abtn.classList.add("opeatorBtn")
        atd7.appendChild(abtn)
        atr.append(atd1, atd2, atd3, atd4, atd5, atd6, atd7)
        tbody.appendChild(atr)

    }
        if(talkTab.childNodes[2])
           talkTab.removeChild(talkTab.childNodes[2])


        talkTab.appendChild(tbody)

}




function deleteTalk(id) {
    $.ajax({
            "url":"/api/talk/delete",
            "method":"GET",
            data:{
                token:  globalData.token,
                id: id,
                userId:  globalData.user_id },
            success:function (data) {
                alert("删除成功")
                init()
            }
        }
    )
}



window.onload = init()